package view.plugin
{
	//全局面板：包括玩家形象，金币和毛豆
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import flash3.framework.LayerManager;
	import flash3.framework.Singleton;
	
	import global.Resource;
	
	import model.GameModel;
	import model.GlobalModel;
	import model.MeInfoModel;
	
	import util.SoundManager;
	
	import view.layer.L;
	import view.plugin.PlayerInfoPanel;
	
	import vo.GameServer;
	import vo.UserInfo;
	import vo.UserScore;
	
	public class GlobalPanel extends Sprite
	{
		private var main:MovieClip = null;
		private var isOpenSound:Boolean = true;
		public static const CLICK_LEAVE:String = "click_leave";
		public function GlobalPanel():void
		{
			super();
			
			main = Resource.i.getMovieClip("top", "Top");
			addChild(main);
			main.gold.icon.gotoAndStop(1);
			main.gold.buttonMode = true;
			main.gold.value.mouseEnabled = false;
			main.maodou.icon.gotoAndStop(2);
			main.btnOpenSound.visible = false;
			main.btnCloseSound.addEventListener(MouseEvent.CLICK, onClickBtnCloseSound);
			main.btnOpenSound.addEventListener(MouseEvent.CLICK, onClickBtnCloseSound);
			main.btnLeave.addEventListener(MouseEvent.CLICK, onClickBtnLeave);
			main.gold.addEventListener(MouseEvent.CLICK, onClickGold);
		}
		private function onMouseOverAvatar(evt:MouseEvent):void
		{
			PlayerInfoPanel.i.visible = true;
			PlayerInfoPanel.i.x = 80;
			PlayerInfoPanel.i.y = 70;
			PlayerInfoPanel.i.setData(MeInfoModel.i.userInfo, MeInfoModel.i.userScore);
		}
		private function onMouseOutAvatar(evt:MouseEvent):void
		{
			PlayerInfoPanel.i.visible = false;
		}
		private function onClickGold(evt:MouseEvent):void
		{
			GlobalModel.navigateToAddMoney();
		}
		private function onClickBtnCloseSound(evt:MouseEvent):void
		{
			isOpenSound = !isOpenSound;
			main.btnCloseSound.visible = isOpenSound;
			main.btnOpenSound.visible = !isOpenSound;
			SoundManager.i.setSoundVolume(isOpenSound ? 1 : 0);
			this.dispatchEvent(evt);
		}
		private function onClickBtnLeave(evt:MouseEvent):void
		{
			var evt1:MouseEvent = new MouseEvent(CLICK_LEAVE);
			this.dispatchEvent(evt1);
		}
		public function updateInfo():void
		{
			var userInfo:UserInfo = MeInfoModel.i.userInfo;
			var userScore:UserScore = MeInfoModel.i.userScore;
			
			if(LayerManager.i.curState == L.L_Lobby)
			{
				main.maodou.visible = false;
			}
			else
			{
				if(LayerManager.i.curState == L.L_Game)
				{
					var gameServer:GameServer = GameModel.i.gameServer;
					main.maodou.icon.gotoAndStop(gameServer.isType(GameServer.GAME_GENRE_RALLY) ? 3 : 2);
				}
				else
				{
					main.maodou.icon.gotoAndStop(2);
				}
				main.maodou.visible = true;
				main.maodou.value.text = userScore.lScore;
			}
			main.txtName.text = userInfo.szAccounts;
			main.gold.value.text = userScore.lGoldScore;
			
			var userImage:UserImage = MeInfoModel.i.userImage;
			userImage.x = 29;
			userImage.y = 13;
			userImage.width = 50;
			userImage.height = 44;
			main.addChild(userImage);
		}
		
		static public function get i():GlobalPanel
		{
			return Singleton.getInstanceOrCreate(GlobalPanel) as GlobalPanel;
		}
	}
}